<template>
	<view class="g-content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="g-content">
			<uni-easyinput placeholder="请输入账号" v-model="account"></uni-easyinput>
      <uni-easyinput placeholder="请输入密码" v-model="password" type="password"></uni-easyinput>
      <button class="login-btn g-button-radius" @click="login">登录</button>
      <view style="width: 80%; margin-top: 20rpx;" class="g-row-horbetween">
        <uni-link text="忘记密码？"></uni-link>
        <uni-link text="注册账号"></uni-link>
      </view>
		</view>
	</view>
</template>

<script>
  import Login from '../../api/Login.js';
	export default {
		data() {
			return {
				account: '',
        password: ''
			}
		},
		onLoad() {
      let id = uni.getStorageSync('id');
      if(id != '' && id != null) {
        this._loginCheck();
      }
		},
		methods: {
      async _login(account, password){
        uni.showLoading();
        const rm = (await Login.login(account, password));
        this.Test.log('登录页',rm);
        if(rm[0] != null){
          this.Toast.show('未知错误');
          uni.hideLoading();
          return;
        }
        // 成功获取登录状态
        if(rm[1]['statusCode'] == '200'){
          if(rm[1]['data'] == [] || rm[1]['data'] == ''){
            this.Toast.show('账号或密码错误，请重新输入');
            uni.hideLoading();
            this.password = '';
            return;
          }
          const rd = rm[1].data[0];
          this.Test.log('登录页',rd);
          this.$store.commit("LOGIN", {
            id: rd['账号'],
            pas: rd['密码'],
            power: rd['身份']
          });
          this.Test.log('test',this.$store.state);
          this.Toast.show('登录成功');
          uni.hideLoading();
          // 任课1 管理2 学生3
          switch(rd['身份']){
            case '1':
              uni.navigateTo({
                url: 'teacher/curriculum',
                fail(e) {
                  console.log(e);
                }
              });
              break;
            case '2':
              uni.navigateTo({
                url: 'adminer/lab'
              });
              break;
            case '3':
              uni.navigateTo({
                url: 'student/today'
              });
              break;
            default:
              break;
          }
        }
      },
      async _loginCheck() {
        this.account = uni.getStorageSync("id");
        this.password = uni.getStorageSync("pas");
        this._login(this.account, this.password);
      },
      login(){
        this._login(this.account, this.password);
      }
		}
	}
</script>

<style scoped>
	.logo {
    width: 400rpx;
    height: 400rpx;
		margin-left: auto;
		margin-right: auto;
    margin-top: 200rpx;
		margin-bottom: 200rpx;
	}
  /deep/ .uni-easyinput {
    width: 80%;
    margin-top: 20rpx;
    border-radius: 40rpx;
  }
  .login-btn {
    height: 90rpx;
    width: 80%;
    margin-top: 20rpx;
    background-color: #EBD428;
    color: #707070;
  }
</style>
